import NProgress from '@/utils/nprogress'
import { createRouter, createWebHistory } from 'vue-router'
import { setSvgColor } from '@/utils/svg'

const title = '小阿紫-给未来写封信'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      meta: { title: '登录页', pageTitle: '登录' },
      component: () => import('@/views/login/LoginPage.vue')
    },
    {
      path: '/reg',
      meta: { pageTitle: '注册' },
      component: () => import('@/views/login/RegPage.vue')
    },
    {
      path: '/resetpwd',
      meta: { pageTitle: '重置密码' },
      component: () => import('@/views/login/ResetPassword.vue')
    },
    {
      path: '/',
      meta: { title: title },
      component: () => import('@/views/letter/LetterPage.vue')
    },
    // 给未来写封信模块
    {
      path: '/letter',
      meta: { pageTitle: '给未来写封信' },
      component: () => import('@/views/letter/IndexPage.vue'),
      redirect: '/letter/index',
      children: [
        {
          path: '/letter/index',
          meta: { pageTitle: '给未来写封信' },
          component: () => import('@/views/letter/LetterPage.vue')
        },
        {
          path: '/letter/write',
          meta: {
            pageTitle: '给未来写封信-一场跨越时空的对话, 想好写什么了吗？'
          },
          component: () => import('@/views/letter/LetterWritePage.vue')
        },
        {
          path: '/letter/write-modify',
          meta: {
            pageTitle: '给未来写封信'
          },
          component: () => import('@/views/letter/LetterWriteModifyPage.vue')
        },
        {
          path: '/letter/pubs',
          meta: { pageTitle: '给未来写封信-公开信箱' },
          component: () => import('@/views/letter/LetterPublicLetterPage.vue')
        },
        {
          path: '/letter/showpub',
          meta: { pageTitle: '给未来写封信' },
          component: () => import('@/views/letter/LetterShowPubPage.vue')
        },
        {
          path: '/letter/showcur',
          meta: { pageTitle: '给未来写封信' },
          component: () => import('@/views/letter/LetterShowCurPage.vue')
        },
        {
          path: '/letter/test',
          meta: { pageTitle: '给未来写封信' },
          component: () => import('@/views/letter/LetterTest.vue')
        },
        {
          path: '/letter/sendok',
          meta: { pageTitle: '给未来写封信-发送成功' },
          component: () => import('@/views/letter/LetterSendOkPage.vue')
        },
        {
          path: '/letter/saveok',
          meta: { pageTitle: '给未来写封信-暂存成功' },
          component: () => import('@/views/letter/LetterSaveOkPage.vue')
        },
        {
          path: '/letter/docs',
          meta: { pageTitle: '给未来写封信-用户协议' },
          component: () => import('@/views/letter/LetterDocsPage.vue'),
          redirect: '/letter/docs/law',
          children: [
            {
              path: '/letter/docs/about',
              component: () => import('@/views/letter/docs/AboutPage.vue')
            },
            {
              path: '/letter/docs/law',
              component: () => import('@/views/letter/docs/LawPage.vue')
            },
            {
              path: '/letter/docs/log',
              component: () => import('@/views/letter/docs/LogPage.vue')
            },
            {
              path: '/letter/docs/public',
              component: () => import('@/views/letter/docs/PublicPage.vue')
            },
            {
              path: '/letter/docs/problem',
              component: () => import('@/views/letter/docs/ProblemPage.vue')
            }
          ]
        },
        {
          path: '/letter/user',
          meta: { pageTitle: '给未来写封信-个人中心' },
          component: () => import('@/views/letter/LetterUserPage.vue'),
          redirect: '/letter/user/all',
          children: [
            {
              path: '/letter/user/all',
              component: () => import('@/views/letter/user/UserAllPage.vue')
            },
            {
              path: '/letter/user/save',
              component: () =>
                import('@/views/letter/user/UserSaveLetterPage.vue')
            },
            {
              path: '/letter/user/list',
              component: () =>
                import('@/views/letter/user/UserLetterListPage.vue')
            }
          ]
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.pageTitle || title // 设置页面标题，如果未设置则使用默认标题

  NProgress.start() // 开启进度条

  // 保持系统颜色
  if (localStorage.getItem('zlcode-page-color')) {
    const isDark = JSON.parse(localStorage.getItem('zlcode-page-color')).isDark
    if (isDark) {
      document.documentElement.classList.add('dark')
      document.documentElement.classList.remove('light')
      document.body.setAttribute('arco-theme', 'dark') // 字节
      if (document.querySelector('#zlcode-bg-id') != null) {
        document.querySelector('#zlcode-bg-id').style.backgroundImage =
          'linear-gradient(to top, #d299c2 0%, #fef9d7 100%)'
      }
    } else {
      document.documentElement.classList.remove('dark')
      document.documentElement.classList.add('light')
      document.body.removeAttribute('arco-theme') // 字节
      if (document.querySelector('#zlcode-bg-id') != null) {
        document.querySelector('#zlcode-bg-id').style.backgroundImage =
          'linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%)'
      }
    }
    setSvgColor()
  }

  next() // 继续导航
})

router.afterEach(() => {
  NProgress.done() // 关闭进进度条
})

export default router
